package com.crazy.web.service.user.impl;

import cn.hutool.core.exceptions.ValidateException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crazy.model.major.entity.TUserLimit;
import com.crazy.model.major.service.TUserLimitService;
import com.crazy.web.service.user.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/12/15 16:23
 */
@Service
public class AuthorityServiceImpl implements AuthorityService {

    @Autowired
    private TUserLimitService tUserLimitService;

    @Override
    public List<TUserLimit> list() {
        QueryWrapper<TUserLimit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TUserLimit.Columns.valid, 1);
        queryWrapper.orderByAsc(TUserLimit.Columns.orderNo);
        return tUserLimitService.list(queryWrapper);
    }

    @Override
    public void deleteById(Long id) {
        QueryWrapper<TUserLimit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TUserLimit.Columns.valid, 1);
        List<TUserLimit> limits = tUserLimitService.list(queryWrapper);
        List<TUserLimit> removeLimits = new ArrayList<>();
        for(TUserLimit limit : limits){
            String[] ids = limit.getLevel().split(":");
            if(Arrays.asList(ids).contains(id.toString())){
                limit.setValid(0);
                removeLimits.add(limit);
            }
        }
        tUserLimitService.updateBatchById(removeLimits);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(TUserLimit tUserLimit) {
        if(tUserLimit.getId() == null){
            //新增
            //先获取parentId的level
            long parentId = tUserLimit.getParentId();
            tUserLimit.setCreateTime(LocalDateTime.now());
            tUserLimit.setUpdateTime(LocalDateTime.now());
            if(!tUserLimitService.save(tUserLimit)){
                throw new ValidateException("添加失败");
            }
            TUserLimit parentLimit = tUserLimitService.getById(parentId);
            tUserLimit.setLevel(parentLimit.getLevel()+":"+tUserLimit.getId());
            if(!tUserLimitService.updateById(tUserLimit)){
                throw new ValidateException("添加失败");
            }
        }else{
            //编辑
            TUserLimit oldLimit = tUserLimitService.getById(tUserLimit.getId());
            oldLimit.setLimitName(tUserLimit.getLimitName());
            oldLimit.setType(tUserLimit.getType());
            oldLimit.setUrl(tUserLimit.getUrl());
            oldLimit.setRequireLimit(tUserLimit.getRequireLimit());
            oldLimit.setOrderNo(tUserLimit.getOrderNo());
            oldLimit.setDescript(tUserLimit.getDescript());
            oldLimit.setUpdateTime(LocalDateTime.now());
            if(!tUserLimitService.updateById(oldLimit)){
                throw new ValidateException("修改失败");
            }
        }
    }

    @Override
    public TUserLimit detail(Long id) {
        TUserLimit tUserLimit = tUserLimitService.getById(id);
        if(tUserLimit!=null && tUserLimit.getValid()!=null && tUserLimit.getValid().intValue() == 1){
            return tUserLimit;
        }else{
            throw new ValidateException("获取权限详情失败");
        }
    }
}
